package cn.ac.ict.debs.testsetGenerator;

import java.io.BufferedReader;
import java.io.FileReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WSLAGenerator extends Generator
{
	public void genreateWSLA() throws Exception
	{
		System.out.println("Beginning creating the wsla file...");
		this.readQosFile();
		this.startBuildingXml();
		for(Integer i : serviceMap.keySet())
		{
			this.addServiceToXml(serviceMap.get(i.intValue()));
		}
		this.saveXml(this.doc, this.DIR + "Servicelevelagreements.wsla");
		System.out.println("End creating the wsla file.");
	}
	
	private void readQosFile() throws Exception
	{
		BufferedReader in = new BufferedReader(new FileReader(this.QOS_FILE_PATH));
		String line = null;
		int id = -1;
		while((line = in.readLine()) != null)
		{
			if(line.startsWith("ID"))
			{
				id = Integer.parseInt(this.getLineValue(line));
				if(id == 0)
				{
					this.currentService = null;
				}
				else this.currentService = serviceMap.get(id);
			}
			else if(line.startsWith("Count"))
			{
				int count = Integer.parseInt(this.getLineValue(line));
				if(this.currentService == null)
				{
					continue;
				}
				this.currentService.setQos(count);
			}
		}
	}
	
	private void startBuildingXml() throws Exception
	{
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		this.doc = builder.newDocument();
		
		//<SLA xmlns="http://www.ibm.com/wsla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="WSC Service SLA">
		rootElement = doc.createElement("SLA");
		doc.appendChild(rootElement);
		rootElement.setAttribute("xmlns", "http://www.ibm.com/wsla");
		rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
		rootElement.setAttribute("name", "WSC Service SLA");
		
		this.obliElement = this.appendChild("Obligations", this.rootElement);
	}
	
	private int getResponseTime(Service s)
	{
		if(s.getQos() == -1)
		{
			System.out.println(s.getId());
		}
		
		//return s.getQos();
		return 100;
	}
	
	private int getThroughput(Service s)
	{
		//return s.getQos();
		return 100;
	}
	
	private void addServiceToXml(Service s)
	{
		/*
		<ServiceLevelObjective name="ServiceLevelObjectiveResponsetimeserv1944750086">
        	<Expression>
            	<Predicate xsi:type="Less">
                  <SLAParameter>SLAParameterResponsetimeserv1944750086</SLAParameter>
                  <Value>390</Value>
             	</Predicate>
        	</Expression>
   		</ServiceLevelObjective>
   		*/
		Element rpElement = this.appendChild("ServiceLevelObjective", this.obliElement);
		rpElement.setAttribute("name", String.format("ServiceLevelObjectiveResponsetimeserv%d", s.getId()));
		Element rpValueElement = this.appendChild("Value", rpElement);
		rpValueElement.appendChild(doc.createTextNode(String.format("%d", this.getResponseTime(s))));
		
		Element tpElement = this.appendChild("ServiceLevelObjective", this.obliElement);
		tpElement.setAttribute("name", String.format("ServiceLevelObjectiveThroughputserv%d", s.getId()));
		Element tpValueElement = this.appendChild("Value", tpElement);
		tpValueElement.appendChild(doc.createTextNode(String.format("%d", this.getThroughput(s))));
		
	}
	
	private Element appendChild(String name, Element parent)
	{
		Element element = this.doc.createElement(name);
		parent.appendChild(element);
		return element;
	}
	
	private Service currentService;
	private Document doc;
	private Element rootElement;
	private Element obliElement;
}
